import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Layout from "../layout/index.vue";
import Login from "../views/login/index.vue";
import Admin from "../views/administrator/index.vue";
import User from "../views/user/index.vue";
import Goods from "../views/goods/index.vue";
import Order from "../views/order/index.vue";
import Banner from "../views/banner/index.vue";
import Offers from "../views/offers/index.vue";
import Stats from "../views/stats/index.vue";
import NoFound from "../views/404.vue";
// 设计成二级路由 一级路由都是相同的
// 路由配置数组叫做路由表  async动态路由表
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/", // /
    component: Layout,
    name: "index",
    meta: {
      til: "管理员管理",
      icon: "House",
    },
    redirect: "/admin/admin",
    children: [
      {
        path: "/admin/admin",
        component: Admin,
        name: "role",
        meta: {
          til: "管理员列表",
          icon: "House",
        },
      },
    ],
  },
  {
    path: "/user", // /
    component: Layout,
    name: "users",
    meta: {
      til: "用户管理",
      icon: "House",
    },
    redirect: "/user/list",
    children: [
      {
        path: "/user/list",
        component: User,
        name: "userlist",
        meta: {
          til: "用户列表",
          icon: "House",
        },
      },
    ],
  },
  {
    path: "/goods", // /
    component: Layout,
    name: "goods",
    meta: {
      til: "商品管理",
      icon: "House",
    },
    redirect: "/goods/list",
    children: [
      {
        path: "/goods/list",
        component: Goods,
        name: "goodslist",
        meta: {
          til: "商品列表",
          icon: "House",
        },
      },
    ],
  },
  {
    path: "/order", // /
    component: Layout,
    name: "order",
    meta: {
      til: "订单管理",
      icon: "House",
    },
    redirect: "/order/list",
    children: [
      {
        path: "/order/list",
        component: Order,
        name: "orderlist",
        meta: {
          til: "订单列表",
          icon: "House",
        },
      },
    ],
  },
  {
    path: "/banner", // /
    component: Layout,
    name: "banner",
    meta: {
      til: "轮播图管理",
      icon: "House",
    },
    redirect: "/banner/list",
    children: [
      {
        path: "/banner/list",
        component: Banner,
        name: "bannerlist",
        meta: {
          til: "轮播图列表",
          icon: "House",
        },
      },
    ],
  },
  {
    path: "/offers", // /
    component: Layout,
    name: "offers",
    meta: {
      til: "优惠券管理",
      icon: "House",
    },
    redirect: "/offers/list",
    children: [
      {
        path: "/offers/list",
        component: Offers,
        name: "offerslist",
        meta: {
          til: "优惠券列表",
          icon: "House",
        },
      },
    ],
  },
  {
    path: "/stats", // /
    component: Layout,
    name: "stats",
    meta: {
      til: "统计图管理",
      icon: "House",
    },
    redirect: "/stats/list",
    children: [
      {
        path: "/stats/list",
        component: Stats,
        name: "statslist",
        meta: {
          til: "统计图列表",
          icon: "House",
        },
      },
    ],
  },
];

// 静态路由表
const staticRoutes: RouteRecordRaw[] = [
  {
    path: "/login",
    component: Login,
    name: "login",
    meta: {
      til: "登录",
    },
  },
  {
    path: "/:catchAll(.*)", //  /user1
    component: NoFound,
    name: "404",
    meta: {
      til: "页面未找到",
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...asyncRoutes, ...staticRoutes],
});
//路由守卫
router.beforeEach((to, _, next) => {
  //设置导航
  document.title = to.meta?.til as string;
  // console.log(to, "执行了 beforeEach");
  let user = localStorage.getItem("user")
    ? JSON.parse(localStorage.getItem("user") as string)
    : {};
  // 判断是否登录
  if (user.salt || to.path == "/login") {
    // console.log("登陆了");
    next();
  } else {
    // console.log(to.path, "没登陆");
    next({ path: "/login", query: { redirect: to.path } });
  }
});
export default router;
